<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html>
<head>
    <base href="<%=basePath%>">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>漫步时尚广场</title>
    <script type="text/javascript" src="js/shoppingCart.js"></script>
    <script type="text/javascript" src="js/jquery-3.4.0.js"></script>
    <link href="css/style.css" rel="stylesheet" type="text/css">
    <style type="text/css">
        li, ul {
            list-style: none
        }

        .xl {
            display: inline-block;
            position: relative
        }

        .xl > .dropdown {
            background: #fff;
            width: 285px;
            height: 225px;
            border: 1px solid #ccc;
            padding: 5px;
            position: absolute;
            right: 0;
            top: 30px;
            z-index: 999;
            overflow-y: auto;
            overflow-x: hidden;
            display: none
        }
    </style>
</head>
<body>
<!--顶部区域 start-->
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="top_line">
    <tr>
        <td bgcolor="#f7f7f7">
            <table width="1200" border="0" cellspacing="0" cellpadding="0" align="center">
                <tr>
                    <td class="padding-top"><img src="images/star.jpg">收藏 | HI，欢迎来订购 ！
                        <c:choose>
                            <c:when test="${not empty bruser.userName}">
                                <a href="#" class="orange">${bruser.userName}&nbsp;&nbsp;</a>
                                <a href="<%=basePath%>/order/toShoppingOrder" class="orange"> 我的订单&nbsp;&nbsp; </a>
                                <a href="<%=basePath%>/user/logOut" class="orange"> 登出</a>
                            </c:when>
                            <c:otherwise>
                                <a href="<%=basePath%>/user/toLogin" class="orange">[请登录]</a>
                                <a href="<%=basePath%>/user/toRegister" class="orange"> [免费注册]</a>
                            </c:otherwise>
                        </c:choose>
                    </td>
                    <td align="right">
                        <!-- <span class="droparrow"><span class="shopcart"></span>我的购物车<span class="orange">0</span>件<img src="images/arrow.gif" /></span> -->
                        <div class="xl">
                            <!--购物车-->
                            <div class="droparrow" onclick="showCar()"><span class="shopcart"></span>我的购物车
                                <!-- <span class="orange">0</span>件 --><img src="images/arrow.gif"/></div>
                            <!--下拉菜单-->
                            <div class="dropdown" id="dropdown" style="display: none"
                                 ondrop="drop(event)" ondragover="allowDrop(event)">

                                <ul class="shop_pic">
                                </ul>
                            </div>
                        </div>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>
<!--顶部区域 end-->
<script type="text/javascript">
    //购买商品
    function buyGoods() {
        var goodNumMap = {};
        //遍历web存储，提交订单
        for (var i = 0; i < localStorage.length; i++) {
            var getKey = localStorage.key(i);
            var jsonStr = localStorage.getItem(getKey);
            var good = JSON.parse(jsonStr);
            goodNumMap[good.id] = good.goodsNum;
        }
        var user = "<%=session.getAttribute("bruser")%>";
        if (user == null || user == "null") {
            alert("您还未登录，为您跳转到登录页面……");
            window.location.href = '<%= basePath%>user/login';
            return;
        }
        $.ajax({
            type: "post",
            url: "<%= basePath%>/order/buyGoods",
            data: JSON.stringify(goodNumMap),
            contentType: "application/json;charsetset=UTF-8",
            /* data:goodNumMap, */
            //dataType:"json",
            success: function (data) {
                if (data) {
                    alert(data + "购买成功，请查看订单");
                    window.location.href = '<%= basePath%>' + data;
                } else {
                    alert("出现错误：" + data.msg);
                }
            },
            error: function (jqXHR) {
                alert("发生错误：" + jqXHR.status);
            }
        });
        //清空web存储
        localStorage.clear();
    }
</script>
</body>
</html>